
package com.mti.monitor.controller;


import com.mti.monitor.bean.model.MonitorPage;
import com.mti.monitor.bean.model.SysDataSourceConfig;
import com.mti.monitor.rst.Result;
import com.mti.monitor.service.SysDataSourceConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Author: hf
 * @Description: 服务心跳数据源
 * @Date: 2024/8/10 15:43
 */
@Api(value = "monitor", description = "monitor")
@RestController
@RequestMapping("/dataSource")
@Slf4j
public class DataSourceController {

    @Autowired
    private SysDataSourceConfigService sysDataSourceConfigService;

    @ApiOperation(value = "查看数据源列表", notes = "查看数据源列表", httpMethod = "GET")
    @GetMapping(value = "/list")
    public List<SysDataSourceConfig> list() {
        return sysDataSourceConfigService.selectAll();
    }

    @ApiOperation(value = "查看数据源列表", notes = "查看数据源列表", httpMethod = "POST")
    @PostMapping(value = "/page")
    public MonitorPage page(@RequestBody MonitorPage page) {
        if (page == null) {
            page.setSize(10);
            page.setStart(1);
        }
        return sysDataSourceConfigService.selectPage(page);
    }


    @ApiOperation(value = "查看数据源", notes = "查看数据源", httpMethod = "GET")
    @GetMapping(value = "/selectById/{id}")
    public SysDataSourceConfig selectById(@PathVariable("id") String id) {
        return sysDataSourceConfigService.selectById(id);
    }

    @ApiOperation(value = "新增数据源配置", notes = "新增数据源配置", httpMethod = "POST")
    @PostMapping(value = "/add")
    public Result<Object> addConfig(@RequestBody SysDataSourceConfig config) {
        sysDataSourceConfigService.create(config);
        return Result.ok();
    }

    @ApiOperation(value = "更新数据源配置", notes = "更新数据源配置", httpMethod = "POST")
    @PostMapping(value = "/update")
    public Result<Object> updateConfig(@RequestBody SysDataSourceConfig config) {
        sysDataSourceConfigService.update(config);
        return Result.ok();
    }

    @ApiOperation(value = "保存数据源配置", notes = "新增数据源配置", httpMethod = "POST")
    @PostMapping(value = "/save")
    public Result<Object> addOrUpdate(@RequestBody SysDataSourceConfig config) {
        if (config.getDataSourceId() == null || StringUtils.isEmpty(config.getDataSourceId())) {
            sysDataSourceConfigService.create(config);
        } else {
            sysDataSourceConfigService.update(config);
        }
        return Result.ok();
    }

    @ApiOperation(value = "删除数据源配置", notes = "删除数据源配置", httpMethod = "GET")
    @GetMapping(value = "/delete/{id}")
    public Result<Object> deleteConfig(@PathVariable("id") String id) {
        sysDataSourceConfigService.delete(id);
        return Result.ok();
    }


    @ApiOperation(value = "查看数据源所有表", notes = "查看数据源所有表", httpMethod = "GET")
    @GetMapping(value = "/getAllTable/{dataSourceId}")
    public Result<Object> getAllTable(@PathVariable("dataSourceId") String dataSourceId) {
        return Result.ok(sysDataSourceConfigService.selectDbTableList(dataSourceId));
    }

    @ApiOperation(value = "查看数据源表的所有列", notes = "查看数据源表的所有列", httpMethod = "GET")
    @GetMapping(value = "/getTableColumnList/{dataSourceId}/{tableName}")
    public Result<Object> getTableColumnList(@PathVariable("dataSourceId") String dataSourceId, @PathVariable("tableName") String tableName) {
        return Result.ok(sysDataSourceConfigService.selectDbTableColumnList(dataSourceId, tableName));
    }

}
